/**    
 * @{#} DoctorActivity.java Create on 2014-5-28 下午9:38:28    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.smiier.skin.adapter.UserAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.utils.CommonUtility;

public class SearchResultActivity extends BasicActivity implements
		OnItemClickListener {

	private UserAdapter doctorAdapter;
	private ListView list_search_doctor_result;

	private TextView text_empty_content;

	private boolean isDoctor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_doctor_result);

		isDoctor = getIntent().getBooleanExtra(Constant.IDENTITY_KEY_1, true);
		init();
		if (isDoctor) {
			setNavTitle("检索医生");
		} else {
			setNavTitle("检索用户");
		}

		text_empty_content = (TextView) findViewById(R.id.text_empty_content);
		list_search_doctor_result = (ListView) findViewById(R.id.list_search_doctor_result);
		list_search_doctor_result.setOnItemClickListener(this);
		loadData(true);

	}

	@Override
	public void onItemClick(AdapterView<?> adapteView, View view, int arg2,
			long arg3) {
		JSONObject object = (JSONObject) view.getTag();
		if (isDoctor) {
			Intent intent = new Intent(activity, DoctorDetailActivity.class);
			intent.putExtra(Constant.IDENTITY_KEY, object.toString());
			startActivity(intent);
		} else {
			Intent intent = new Intent(activity, QuestionDetailActivity.class);
			try {
				intent.putExtra(Constant.IDENTITY_KEY, object.getString("User"));
			} catch (Exception e) {
				e.printStackTrace();
			}
			intent.putExtra(Constant.IDENTITY_KEY_1, true);
			startActivity(intent);
		}
	}

	public void loadData(boolean isShowDialog) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put(Constant.PARAM_API, "User.GetList");
		params.put("User_Type", isDoctor ? 2 : 1);
		// params.put("Default", 0);
		// params.put("Is_Cert", -1);
		params.put("KeyWord", getIntent().getStringExtra(Constant.IDENTITY_KEY));
		RequestTaskIntercept.requestIntercept(activity, Constant.URL.REQUEST,
				new RequestHandler(activity, new HandlerCallback() {

					@Override
					public void callback(Object o) {
						if (!CommonUtility.isNull(o)) {
							try {
								JSONObject object = (JSONObject) o;
								if (CommonUtility.response200(object)) {
									object = object.getJSONObject("Res");
									JSONArray array = object
											.getJSONArray("Users");
									if (array.length() > 0) {
										text_empty_content
												.setVisibility(View.GONE);
									} else {
										text_empty_content
												.setVisibility(View.VISIBLE);
									}
									ArrayList<Object> list = new ArrayList<Object>();
									CommonUtility.putAll(list, array, true);
									if (CommonUtility.isNull(doctorAdapter)) {
										doctorAdapter = new UserAdapter(
												activity, list, isDoctor);
										list_search_doctor_result
												.setAdapter(doctorAdapter);
									} else {
										doctorAdapter.notifyUpdate();
									}
								} else {
									CommonUtility.toast(activity,
											R.string.server_unavailable);
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}), isShowDialog, params);
	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */

}
